<?php

namespace app\admin\controller;

use think\Controller;

class Base extends Controller
{
    function _initialize()
    {

        //判断用户是否登陆
        if (empty(session('id'))) {
            $this->redirect(url('index/index'));
        }

        $request = request();

        //判断前台，技师，权限是否逾越
        $action = $request->action();
        $roles = session('role');
        // p(config('tech_modules'));
        switch ($roles) {
            case '16':// 接待人员
                if (in_array(strtolower($action), config('tech_modules'))) {
                    $this->error('没有相关权限');
                }
                break;
            case '8':// 维修技师
                if (in_array(strtolower($action), config('front_modules'))) {
                    $this->error('没有相关权限');
                }
                break;
        }

        if ($roles == 0) {
            //获取系统的菜单数据，拼凑成属性结构
            $menus = model('menu')->order('sortnum')->select();
            $menusRes = build_tree($menus, 0);
            $this->assign("menus", $menusRes);
            //p($menusRes);

            //当前菜单数据
            $fullAction = '/' . $request->module() . '/' . $request->controller() . '/' . $request->action();
            $indexAction = '/' . $request->module() . '/' . $request->controller() . '/index';
            $currentMenu = model('menu')->where(['url' => $fullAction])->find();
            if ($currentMenu) {
                $this->assign('currentMenu', $currentMenu);
            } else {
                $currentMenu = model('menu')->where(['url' => $indexAction])->find();
                $this->assign('currentMenu', $currentMenu);
            }
        }
    }

    public function _empty()
    {
        $this->redirect(url('index/p404'));
    }
}
